int maxDepth(struct TreeNode* root) {
    return root ? 1 + fmax(maxDepth(root->left), maxDepth(root->right)) : 0;
}

bool isBalanced(struct TreeNode* root) {
    if (root == NULL)
        return true;
    int left = maxDepth(root->left);
    int right = maxDepth(root->right);
    return abs(left - right) < 2
        && isBalanced(root->left)
        && isBalanced(root->right);
}